################################################################################
# Imports
################################################################################

import Education
from EducationGUIHelper import *
import pprint #TODO: Remove me
import ycp
ycp.import_module("SCR")
from ycp import SCR, Path

################################################################################
# Presets
################################################################################

# Need to filter port 80 access to messenger.hotmailcom and webmessenger.msn.com as well
protocols = {
              'web': {
                'friendly_name': 'Web',
                'ports': {'80':'tcp', '443':'tcp'}
              },          
              'ftp': {
                'friendly_name': 'File transfer (FTP)',
                'ports': {'20':'tcp', '21':'tcp'}
              },
              'yahoo': {
                'friendly_name': 'Yahoo! Messenger',
                'ports': {'5050':'tcp'}
              },
              'aim': {
                'friendly_name': 'AOL Instant Messenger',
                'ports': {'5190':'tcp'}
              },
               'msn': {
                'friendly_name': 'MSN Messenger',
                'ports': {'1863':'tcp', '6891-6901':'both', '1503':'both', '6571':'both' }
              },
              'tftp': {
                'friendly_name': 'TFTP',
                'ports': {'69':'udp'}
              },
              'nfs': {
                'friendly_name': 'NFS',
                'ports': {'1025':'tcp'}
              },
              'icq': {
                'friendly_name': 'ICQ',
                'ports': {'5190':'tcp'}
              },
              'gnutella': {
                'friendly_name': 'Gnutella',
                'ports': {'6346':'both'}
              },
              'bittorrent': {
                'friendly_name': 'BitTorrent',
                'ports': {'6881-6968':'both', '6969':'tcp', '6970-6999':'both'}
              },
              'irc': {
                'friendly_name': 'IRC',
                'ports': {'194':'udp', '6660-6669':'tcp', '6679':'tcp', '6697':'tcp'}
              },
              'telnet': {
                'friendly_name': 'Telnet',
                'ports': {'23':'tcp', '107':'tcp', '992':'both'}
              },
              'smtp': {
                'friendly_name': 'Email (SMTP)',
                'ports': {'25':'tcp', '465':'tcp', '587':'tcp'}
              },
              'pop': {
                'friendly_name': 'Email (POP)',
                'ports': {'109':'tcp', '110':'tcp', '995':'tcp', '1109':'tcp'}
              },
              'rdp': {
                'friendly_name': 'RDP',
                'ports': {'3389':'tcp'}
              },
              'imap': {
                'friendly_name': 'Email (IMAP)',
                'ports': {'143':'both', '220':'both', '993':'tcp'}
              },
              'ssh': {
                'friendly_name': 'SSH',
                'ports': {'22':'both'}
              },
              'nntp': {
                'friendly_name': 'News (NNTP)',
                'ports': {'119':'tcp', '563':'both'}
              },
              'vnc': {
                'friendly_name': 'VNC',
                'ports': {'5800-5810':'tcp', '5900-5910':'tcp'}
              }
            }

# For convenience
reverse_index = {
                    'Web': 'web',
                    'File transfer (FTP)': 'ftp',
                    'Yahoo! Messenger': 'yahoo',
                    'AOL Instant Messenger': 'aim',
                    'MSN Messenger': 'msn',
                    'TFTP': 'tftp',
                    'NFS': 'nfs',
                    'ICQ': 'icq',
                    'Gnutella': 'gnutella',
                    'BitTorrent': 'bittorrent',
                    'IRC': 'irc',
                    'Telnet': 'telnet',
                    'Email (SMTP)': 'smtp',
                    'Email (POP)': 'pop',
                    'RDP': 'rdp',
                    'Email (IMAP)': 'imap',
                    'SSH': 'ssh',
                    'News (NNTP)': 'nntp',
                    'VNC': 'vnc'
                }

################################################################################
# Functions
################################################################################

class FirewallRules:

  def __init__(self):

    self.blocked_protocols = []
    self.modified = False

  def add_blocked_protocol(self, protocol, loading=False):
    print "Add " + protocol
    if (protocol in reverse_index.keys()) and (not reverse_index[protocol] in self.blocked_protocols):
      print reverse_index[protocol]
      self.blocked_protocols.append(reverse_index[protocol])

      if not loading:
        self.modified = True

  def add_blocked_protocol_by_key(self, protocol, loading=False):

     if (protocol in protocols.keys()) and (not protocol in self.blocked_protocols):
      self.blocked_protocols.append(protocol)

      if not loading:
        self.modified = True


  def remove_blocked_protocol(self, protocol, loading=False):

    if (protocol in reverse_index.keys()) and (reverse_index[protocol] in self.blocked_protocols):
      self.blocked_protocols.remove(reverse_index[protocol])

      if not loading:
        self.modified = True      

  def get_blocked_protocol_keys(self):
    protocol_list = list(self.blocked_protocols)
    protocol_list.sort()
    return protocol_list

  def get_blocked_protocols(self):
    protocol_list = []

    for protocol in self.blocked_protocols:
      protocol_list.append(protocols[protocol]['friendly_name'])

    protocol_list.sort()
    print protocol_list
    return protocol_list

  def write_to_disk(self, group_name):
    path = ".edugroup.value." + group_name
    
    SCR.Write(Path(path + ".BLOCKED_PROTOCOLS"), ",".join(self.blocked_protocols))

    #TODO: Configure firewall rules on disk here
    
def get_protocols_list():

  protocol_list = reverse_index.keys()
  protocol_list.sort()
  return protocol_list

def get_protocol_key_list():

  protocol_list = protocols.keys()
  protocol_list.sort()
  return protocol_list

def get_firewall_rules_for_group(group):
  if (Education.get_config('groups').has_key(group)):
    print Education.get_config('groups')[group].firewall_rules.blocked_protocols
    return Education.get_config('groups')[group].firewall_rules
  else:
    return FirewallRules()